//
//  InternationalControl.m
//  New_AICO
//
//  Created by shansander on 15/2/6.
//  Copyright (c) 2015年 shansander. All rights reserved.
//

#import "InternationalControl.h"


NSString * const language_chinese_model = @"cn";

NSString * const language_en_model = @"en";

@implementation InternationalControl

static NSBundle *bundle = nil;

static NSDictionary * LanguageTypeInfo = nil;

static NSBundle *systemLocalizedbundle = nil;

static NSDictionary *systemLocalizedDict = nil;




+ ( NSBundle * )bundle{
    
    return bundle;
    
}
+ (NSString *)getSystemPreferedLanguage
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    //获取系统当前语言版本(中文zh-Hans,英文en
    
    NSArray* languages = [def objectForKey:@"AppleLanguages"];
    
    NSString *current = [languages objectAtIndex:0];
    return current;
}


+(void)initUserLanguage{
    NSString *current = [InternationalControl getSystemPreferedLanguage];
    [InternationalControl retrieveLanguageFileByLanguage:current];
}

+(void)retrieveLanguageFileByLanguage:(NSString *)languageName{
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:languageName ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:path];//生成bundle
    if (!bundle) {
        // 默认en
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        bundle = [NSBundle bundleWithPath:path];
    }
}



+(NSString *)localizedStringForKey:(NSString * )Key
{
    NSString * title = [bundle localizedStringForKey:Key value:nil table:@"SBooksLanguage"];
    if (!title) {
        title = @"";
    }
    return title;
}




+(NSString *)systemLocalizedStringForKey:(NSString * )Key
{
    if (systemLocalizedDict) {
        NSString * title = systemLocalizedDict[Key];
        if (title) {
            return title;
        }
    }
    NSString * title = [systemLocalizedbundle localizedStringForKey:Key value:nil table:@"AicoLanguage"];
    return title;
}

@end
